import {
  _decorator,
  Button,
  Component,
  instantiate,
  Label,
  Node,
  Prefab,
} from "cc";
import { ItemStatus, ItemType, ItemType2Label } from "./model";
import { DataManager } from "./runtime/data-manager";
import { RenderManager } from "./base/render-manager";

const { ccclass, property } = _decorator;

@ccclass("BackPackManager")
export class BackPackManager extends RenderManager {
  @property(Prefab)
  public keyPrebab: Prefab;

  @property(Prefab)
  public mailPrebab: Prefab;

  @property(Node)
  public placeHolder: Node;

  @property(Label)
  public label: Label;

  @property(Button)
  public btnLeft: Button;

  @property(Button)
  public btnRight: Button;

  @property(Node)
  public hand: Node;

  start() {
    super.start();
  }

  private static _instance: BackPackManager = new BackPackManager();

  public static get Instance(): BackPackManager {
    return this._instance;
  }

  get selected() {
    return DataManager.Instance.itemSelected;
  }

  select() {
    DataManager.Instance.selectItem();
  }

  cancelSelect() {
    DataManager.Instance.cancelSelect();
  }

  getPackItems() {
    return DataManager.Instance.getPackItems();
  }

  getItemByType(type: ItemType) {
    return DataManager.Instance.items.find((item) => item.type === type);
  }

  collectItem(type: ItemType) {
    DataManager.Instance.collectItem(type);
  }

  useItem(type: ItemType) {
    DataManager.Instance.useItem(type);
  }

  isItemInScene(type: ItemType) {
    return DataManager.Instance.items.some(
      (item) => item.type === type && item.status === ItemStatus.InScene
    );
  }

  isItemInPack(type: ItemType) {
    return DataManager.Instance.items.some(
      (item) => item.type === type && item.status === ItemStatus.InPack
    );
  }

  isItemInHand(type: ItemType) {
    return this.selected && DataManager.Instance.curItemType === type;
  }

  render() {
    this.placeHolder.removeAllChildren();

    const itemsInPack = this.getPackItems();
    if (itemsInPack.length === 0) {
      this.node.active = false;
      return;
    }

    this.node.active = true;
    this.hand.active = this.selected;
    if (DataManager.Instance.curItemType === null) {
      DataManager.Instance.setCurItemType(itemsInPack[0].type);
    }
    const curItemIndex = itemsInPack.findIndex(
      (item) => item.type === DataManager.Instance.curItemType
    );
    this.btnLeft.interactable = curItemIndex > 0;
    this.btnRight.interactable = curItemIndex < itemsInPack.length - 1;
    this.generateItem(DataManager.Instance.curItemType);
  }

  generateItem(itemType: ItemType) {
    const itemType2Prefab = {
      [ItemType.Key]: this.keyPrebab,
      [ItemType.Mail]: this.mailPrebab,
    };

    const item = instantiate(itemType2Prefab[itemType]);
    this.placeHolder.addChild(item);
    this.label.string = ItemType2Label[itemType];
  }

  handlerLeftBtnClick() {
    const itemsInPack = this.getPackItems();
    if (itemsInPack.length > 0) {
      const curItemIndex = itemsInPack.findIndex(
        (item) => item.type === DataManager.Instance.curItemType
      );
      if (curItemIndex > 0) {
        DataManager.Instance.setCurItemType(itemsInPack[curItemIndex - 1].type);
      }
      this.cancelSelect();
    }
  }

  handlerRightBtnClick() {
    const itemsInPack = this.getPackItems();
    if (itemsInPack.length > 0) {
      const curItemIndex = itemsInPack.findIndex(
        (item) => item.type === DataManager.Instance.curItemType
      );
      if (curItemIndex < itemsInPack.length - 1) {
        DataManager.Instance.setCurItemType(itemsInPack[curItemIndex + 1].type);
      }
      this.cancelSelect();
    }
  }
}
